//
//  yueShaoOrderViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/17.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "yueShaoOrderViewController.h"
//通过判断来确定出跳转到哪个界面
#import "tiaoZhuanView.h"
#import "yueShaoViewController.h"
#import "yueShaoDetailViewController.h"
#import "HttpRequest.h"
#import "yueSao01.h"
#import "yueSaoOrder.h"
#import "UIImageView+WebCache.h"
#import "yueShaoOrderFinish.h"
#import "orderFinishViewController.h"
//通过判断来确定出跳转到哪个界面
#import "tiaoZhuanView.h"
#import "yueSaoDetail.h"
#import "Macros.h"
#import "TCLYUser.h"
#import "SVProgressHUD.h"

@interface yueShaoOrderViewController ()
//头像背景颜色
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
//提交订单按钮背景色
@property (weak, nonatomic) IBOutlet UIButton *summitBtn;

@property(nonatomic,copy)NSMutableArray*json_Array1;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneNumber;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
//催乳师的阿姨
@property (weak, nonatomic) IBOutlet UILabel *otherNameLabel;
//钱数
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
//头像
@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
//
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel01;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel02;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property(nonatomic,copy)NSMutableArray *json_Array2;
//更改网络地址(确定是催乳师接口,还是月嫂接口)
@property(nonatomic,copy)NSString *urlString1;
@property(nonatomic,copy)NSMutableDictionary *params1;
@property(nonatomic,copy)NSString *urlString2;
@property(nonatomic,copy)NSMutableDictionary *params2;
@property(nonatomic,retain)UIView *backView;
@end

@implementation yueShaoOrderViewController
@synthesize json_Array1,json_Array2,urlString1,urlString2,params1,params2;

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=YES;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    json_Array1=[NSMutableArray array];
    json_Array2=[NSMutableArray array];
    self.view.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:143/255.0 blue:177/255.0 alpha:1.0];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1.0];
    self.moneyLabel01.textColor=[UIColor colorWithRed:244/255.0 green:143/255.0 blue:177/255.0 alpha:1.0];
    self.moneyLabel02.textColor=[UIColor colorWithRed:244/255.0 green:143/255.0 blue:177/255.0 alpha:1.0];
    self.moneyLabel.textColor=[UIColor colorWithRed:244/255.0 green:143/255.0 blue:177/255.0 alpha:1.0];
    [self.summitBtn setBackgroundColor:[UIColor colorWithRed:216/255.0 green:26/255.0 blue:96/255.0 alpha:1.0]];
    [self getDataFromNet];
    self.backView=[[UIView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight-64)];
    self.backView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.backView];
}
//从服务器获取数据
-(void)getDataFromNet
{
    
    yueSao01 *ys01=[yueSao01 sharedManager];
    tiaoZhuanView *tzv=[tiaoZhuanView shardeManager];
    NSString*str=ys01.idString;
    if (tzv.yueShao==1)
    {
        //月嫂
        urlString1=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/Appoint/appointment"];
        params1= [NSMutableDictionary dictionary];
        //    NSLog(@"strstrstrstr=%@",str);
        params1[@"pid"] = str;
        //**********************************************
        //用户注册的姓名(id)
        params1[@"uid"] =userID;
        //**********************************************
    }else{
        urlString1=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/appoint/appointment"];
        params1= [NSMutableDictionary dictionary];
        //    NSLog(@"strstrstrstr=%@",str);
        params1[@"pid"] = str;
        //**********************************************
        //用户注册的姓名(id)
        params1[@"uid"] = userID;
        //**********************************************
    }
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString1 parameters:params1 success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//         yueSaoOrder *yso=[yueSaoOrder shardeManager];
         //获取数组
         json_Array1=[json objectForKey:@"data"];
         if (json_Array1.count>0)
         {
             [self dismiss];
//             yso.address=[json_Array1[0] objectForKey:@"address"];
//             yso.currentprice=[json_Array1[0] objectForKey:@"currentprice"];
//             yso.pic=[json_Array1[0] objectForKey:@"pic"];
//             yso.tel=[json_Array1[0] objectForKey:@"tel"];
//             yso.pname=[json_Array1[0] objectForKey:@"pname"];
//             yso.username=[json_Array1[0] objectForKey:@"username"];
//             self.addressLabel.text=yso.address;
//             self.moneyLabel.text=yso.currentprice;
//             self.moneyLabel01.text=yso.currentprice;
//             self.moneyLabel02.text=yso.currentprice;
//             self.otherNameLabel.text=yso.pname;
//             self.nameLabel.text=yso.username;
//             self.phoneNumber.text=yso.tel;
         }
//         NSURL* imagePath = [NSURL URLWithString:yso.pic];
//         //给一张默认图片，先使用默认图片，当图片加载完成后再替换
//         [self.headerImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
//          NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
  
}
- (void)dismiss
{
    [self.backView removeFromSuperview];
    [SVProgressHUD dismiss];
    yueSaoOrder *yso=[yueSaoOrder shardeManager];
    yso.address=[json_Array1[0] objectForKey:@"address"];
    yso.currentprice=[json_Array1[0] objectForKey:@"currentprice"];
    yso.pic=[json_Array1[0] objectForKey:@"pic"];
    yso.tel=[json_Array1[0] objectForKey:@"tel"];
    yso.pname=[json_Array1[0] objectForKey:@"pname"];
    yso.username=[json_Array1[0] objectForKey:@"username"];
    self.addressLabel.text=yso.address;
    self.moneyLabel.text=yso.currentprice;
    self.moneyLabel01.text=yso.currentprice;
    self.moneyLabel02.text=yso.currentprice;
    self.otherNameLabel.text=yso.pname;
    self.nameLabel.text=yso.username;
    self.phoneNumber.text=yso.tel;
    NSURL* imagePath = [NSURL URLWithString:yso.pic];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [self.headerImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
}
//更改详细地址
- (IBAction)changeAddress:(id)sender
{
    NSLog(@"更改详细地址");
}
- (IBAction)goBack:(id)sender
{
    //跳转到月嫂订单界面
    tiaoZhuanView *tzv=[tiaoZhuanView shardeManager];
    int num=tzv.yueShaoViewNumber;
    if (num==1)
    {
        //返回到月嫂列表界面
        yueShaoViewController *ysc=self.navigationController.viewControllers[1];
        [self.navigationController popToViewController:ysc animated:YES];
        
    }else if (num==2)
    {
    //返回到月嫂详细界面
        yueShaoDetailViewController *ysdvc=self.navigationController.viewControllers[2];
        [self.navigationController popToViewController:ysdvc animated:YES];
    }
}
//提交订单的方法
- (IBAction)submmitToOrder:(id)sender
{
    [self sendMessageToNet];
}
-(void)sendMessageToNet
{

    yueSao01 *ys01=[yueSao01 sharedManager];
    yueSaoOrder *yso=[yueSaoOrder shardeManager];
    tiaoZhuanView *tzv=[tiaoZhuanView shardeManager];
    yueSaoDetail *ysd=[yueSaoDetail shardeManager];
    NSString*str=ys01.idString;
    if (tzv.yueShao==1)
    {
        //月嫂
        urlString2=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/Appoint/appointadd"];
        params2 = [NSMutableDictionary dictionary];
        params2[@"id"] = str;
        params2[@"username"] = yso.username;
        params2[@"phone"] = yso.tel;
        params2[@"address"] = yso.address;
        params2[@"pname"] = yso.pname;
        params2[@"pic"] = yso.pic;
        params2[@"price"] = yso.currentprice;
        params2[@"content"] = self.textField.text;
        //**********************************************
        //用户注册的姓名(id)
        params2[@"uid"] = userID;
        //**********************************************
   
    }else{
        //催乳师
        urlString2=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/appoint/appointadd"];
        params2 = [NSMutableDictionary dictionary];
        params2[@"id"] = str;
        params2[@"username"] = yso.username;
        params2[@"phone"] = yso.tel;
        params2[@"address"] = yso.address;
        params2[@"pname"] = yso.pname;
        params2[@"pic"] = yso.pic;
        params2[@"price"] = yso.currentprice;
        params2[@"content"] = self.textField.text;
        //**********************************************
        //用户注册的姓名(id)
        params2[@"uid"] = userID;
        params2[@"type"]=ysd.type;
        NSLog(@"ysd.typeysd.type=%@",ysd.type);
        //**********************************************
 
    }
//    NSString *urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/admin/Appoint/appointadd"];
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    params[@"id"] = str;
//    params[@"username"] = yso.username;
//    params[@"phone"] = yso.tel;
//    params[@"address"] = yso.address;
//    params[@"pname"] = yso.pname;
//    params[@"pic"] = yso.pic;
//    params[@"price"] = yso.currentprice;
//    params[@"content"] = self.textField.text;
////**********************************************
//    //用户注册的姓名(id)
//    params[@"uid"] = @"2";
//    
////**********************************************

    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString2 parameters:params2 success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //获取数组
         json_Array2=[json objectForKey:@"data"];
         if (json_Array2.count>0)
         {
             yueShaoOrderFinish *ysof=[yueShaoOrderFinish shardeManager];
             ysof.time=[json_Array2[0] objectForKey:@"time"];
             ysof.username=[json_Array2[0] objectForKey:@"username"];
//             NSLog(@"ysof.usernameysof.username=%@",ysof.username);
         }
         orderFinishViewController*ofvc=[orderFinishViewController new];
         [self.navigationController pushViewController:ofvc animated:YES];
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
